define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    //设置弹窗宽高
    Fast.config.openArea = ['70%', '85%'];

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'weixin/reply/index' + location.search,
                    add_url: 'weixin/reply/add',
                    edit_url: 'weixin/reply/edit',
                    del_url: 'weixin/reply/del',
                    multi_url: 'weixin/reply/multi',
                    table: 'weixin_reply',
                }
            });

            var table = $("#table");

            //修改快速搜索显示文字
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "输入ID快速搜索"};

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false, sortable: true},
                        {field: 'keyword', title: __('Keyword'), operate: 'LIKE', formatter: function(val){
                            if (val == 'default' || val == 'subscribe') {
                                val = '<span style="color: red">' + __(val) + '</span>';
                            } else {
                                val = Table.api.formatter.label(val)
                            }
                            return val;
                        }},
                        {field: 'reply_type', title: __('Reply_type'), searchList: {"text":__('Reply_type text'),"image":__('Reply_type image'),"news":__('Reply_type news'),"voice":__('Reply_type voice'),"video":__('Reply_type video')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, operate: false, formatter: Table.api.formatter.toggle, yes: 'normal', no: 'hidden'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            //隐藏tab切换的搜索框
            $('select[name="reply_type"]').parents('.form-group').hide();

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                //修改的时候清空资源ID
                $('input[name="row[content][image]"]').on('change', (function () {
                    $('input[name="row[content][image_media_id]"]').val('');
                }));

                //修改的时候清空资源ID
                $('input[name="row[content][voice]"]').on('change', (function () {
                    $('input[name="row[content][voice_media_id]"]').val('');
                }));

                //修改的时候清空资源ID
                $('input[name="row[content][video]"]').on('change', (function () {
                    $('input[name="row[content][video_media_id]"]').val('');
                }));

                $(document).ready(function(){
                    $('.form-horizontal').removeClass('hide');
                });
            }
        }
    };
    return Controller;
});